<?php

declare(strict_types=1);

namespace app\api\controller\v1;

use think\facade\Db;
use think\Request;

class Goods
{
    /**
     * 显示订单详情
     *
     * @param  int $id
     * @return \think\Response
     */
    public function info($id)
    {
        $order = Db::name('ea_mall_goods')->where('id', $id)->find();
        if ($order) {
            $code = 200;
            $msg = '获取成功';
            $data = $order;
        } else {
            $code = 404;
            $msg = '订单不存在';
            $data = null;
        }
        return result($data, $msg, $code);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->param();
        $result = Db::table('ea_mall_goods')->save($data);

        if ($result == 1) {
            $data = [
                $data,
            ];
            $code = 201;
            $msg = '创建成功';
            return result($data, $msg, $code);
        }
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = Db::table('ea_mall_goods')->select();
        $data = [
            $data,
        ];
        $code = 200;
        $msg = '获取成功';
        return result($data, $msg, $code);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->param();
        $result = Db::table('ea_mall_goods')->where('id', $id)->update($data);

        if ($result == 1) {
            $data = [
                $data,
            ];
            $code = 200;
            $msg = '修改成功';
            return result($data, $msg, $code);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $data = Db::table('ea_mall_goods')->where('id', $id)->find();
        $result2 = Db::table('ea_mall_goods')->where('id', $id)->delete();

        if ($result2 == 1) {
            $data = [
                $data,
            ];
            $code = 202;
            $msg = '删除成功';
            return result($data, $msg, $code);
        }
    }
}
